package com.project.service.impl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.*;
import com.project.mapper.IOutCarMapper;
import com.project.mapper.IOutDetailMapper;
import com.project.mapper.IOutOrderMapper;
import com.project.mapper.IStorageMapper;
import com.project.service.IOutCarService;
import com.project.service.IOutOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OutCarServiceImpl implements IOutCarService {
    @Autowired
    private IOutCarMapper outCarMapper;
    @Autowired
    private IOutOrderMapper orderMapper;
    @Autowired
    private IStorageMapper storageMapper;

    @Autowired
    private IOutDetailMapper detailMapper;

    @Override
    public Integer sumPrice(Integer userId) {
        return outCarMapper.sumPrice(userId);
    }

    @Override
    public List<OutCarBean> findByUserId(Integer userId) {
        return outCarMapper.findByUserId(userId);
    }

    @Override
    public void add(StorageBean storageBean, Integer userId) {

        QueryWrapper<OutCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId", storageBean.getId());
        OutCarBean car = outCarMapper.selectOne(qw);
        if (car == null) {
            OutCarBean outCarBean = new OutCarBean();
            outCarBean.setProductId(storageBean.getId());
            outCarBean.setUserId(userId);
            outCarBean.setNum(1);
            outCarMapper.insert(outCarBean);
        } else {
            car.setNum(car.getNum() + 1);
            outCarMapper.updateById(car);
        }

    }

    @Override
    public void update(Integer carId, Integer newNum) {
        OutCarBean outCarBean = outCarMapper.selectById(carId);
        outCarBean.setNum(newNum);
        outCarMapper.updateById(outCarBean);
    }

    @Override
    public void del(Integer id) {
        outCarMapper.deleteById(id);
    }

    @Override
    public void outStorage(Integer userId, OutOrderBean outOrderBean) {
        outOrderBean.setTotal(outCarMapper.sumPrice(userId));
        orderMapper.insert(outOrderBean);
        List<OutCarBean> carBeanList = outCarMapper.findByUserId(userId);
        for (OutCarBean car :
                carBeanList) {
            OutDetailBean detailBean = new OutDetailBean();
            detailBean.setProductId(car.getProductId());
            detailBean.setNum(car.getNum());
            detailBean.setPrice(car.getProductBean().getoPrice());
            detailBean.setOrderId(outOrderBean.getId());
            detailMapper.insert(detailBean);
            StorageBean storageBean = storageMapper.selectById(car.getProductId());
            storageBean.setNum(storageBean.getNum() - car.getNum());
            storageMapper.updateById(storageBean);
        }
        QueryWrapper<OutCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId);
        outCarMapper.delete(qw);

    }
}
